package com.swq.java.code.chapter19.code21;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

public class Draw2D extends JPanel {
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        double leftX = 50;
        double topY = 35;
        double width = 100;
        double height = 75;

        //定义一个矩形，它的左上角的坐标为（leftX,topY），长和宽分别为width和height
        Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
        g2.draw(rect);  //画一个矩形

        //定义一个椭圆
        Ellipse2D ellipse = new Ellipse2D.Double();
        ellipse.setFrame(rect);  //以矩形作为椭圆的框架
        g2.setColor(Color.red);
        g2.fill(ellipse);  //填充一个红色的椭圆

        //定义一条线，线的两端的坐标分别为（leftX,topY）和（leftX+width,topY+height）
        Line2D line = new Line2D.Double(leftX, topY, leftX + width, topY + height);
        g2.setColor(Color.black);
        //在矩形中画一条黑色的对角线
        g2.draw(line);  //画一条线

        double radius = 70;  //圆的半径
        double centerX = rect.getCenterX();  //矩形的中心的X坐标
        double centerY = rect.getCenterY();  //矩形的中心的Y坐标
        double cornerX = centerX - radius;
        double cornerY = centerY - radius;

        //定义一个矩形，它的左上角的坐标为（cornerX,cornerY），长和宽为2*radius
        rect = new Rectangle2D.Double(cornerX, cornerY, 2 * radius, 2 * radius);
        g2.draw(rect);  //画一个矩形

        //定义一个圆，它所在的矩形框架的左上角的坐标为（cornerX,cornerY），矩形框架的长和宽为2*radius
        Ellipse2D circle = new Ellipse2D.Double(cornerX, cornerY, 2 * radius, 2 * radius);
        g2.draw(circle);  //画一个圆
    }

    public static void main(String[] args) {
        Toolkit t = Toolkit.getDefaultToolkit();
        Dimension d = t.getScreenSize();  //获得屏幕的大小

        int screenWidth = (int) d.getWidth(); //获得屏幕的宽
        int screenHeight = (int) d.getHeight();  //获得屏幕的高

        JFrame frame = new JFrame("Hello");
        Draw2D panel = new Draw2D();
        frame.add(panel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(screenWidth / 4, screenHeight / 4);
        frame.setLocation(screenWidth / 4, screenHeight / 4);
        frame.setVisible(true);
    }
}